"""
用户相关的Pydantic模式
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, ConfigDict

# 基础用户模式
class UserBase(BaseModel):
    """用户基础模式"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: EmailStr = Field(..., description="邮箱地址")
    full_name: Optional[str] = Field(None, max_length=100, description="真实姓名")
    organization: Optional[str] = Field(None, max_length=100, description="组织机构")
    phone: Optional[str] = Field(None, max_length=20, description="手机号码")
    bio: Optional[str] = Field(None, max_length=500, description="个人简介")


class UserCreate(UserBase):
    """用户创建模式"""
    password: str = Field(..., min_length=8, max_length=100, description="密码")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "username": "john_doe",
                "email": "john@example.com",
                "full_name": "John Doe",
                "password": "secretpassword123",
                "organization": "ACME Corp"
            }
        }
    )


class UserUpdate(BaseModel):
    """用户更新模式"""
    full_name: Optional[str] = Field(None, max_length=100, description="真实姓名")
    organization: Optional[str] = Field(None, max_length=100, description="组织机构")
    phone: Optional[str] = Field(None, max_length=20, description="手机号码")
    bio: Optional[str] = Field(None, max_length=500, description="个人简介")
    avatar_url: Optional[str] = Field(None, max_length=255, description="头像URL")


class UserInDB(UserBase):
    """数据库中的用户模式"""
    id: int
    password_hash: str
    role: str
    is_active: bool
    avatar_url: Optional[str] = None
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None
    
    model_config = ConfigDict(from_attributes=True)


class User(UserBase):
    """用户响应模式"""
    id: int
    role: str
    is_active: bool
    avatar_url: Optional[str] = None
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None
    
    model_config = ConfigDict(from_attributes=True)


class UserLogin(BaseModel):
    """用户登录模式"""
    username: str = Field(..., description="用户名或邮箱")
    password: str = Field(..., description="密码")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "username": "john_doe",
                "password": "secretpassword123"
            }
        }
    )


class UserRegister(UserCreate):
    """用户注册模式"""
    confirm_password: str = Field(..., description="确认密码")
    
    def passwords_match(self) -> bool:
        """验证密码是否匹配"""
        return self.password == self.confirm_password


class Token(BaseModel):
    """令牌模式"""
    access_token: str = Field(..., description="访问令牌")
    refresh_token: str = Field(..., description="刷新令牌")
    token_type: str = Field(default="bearer", description="令牌类型")
    expires_in: int = Field(..., description="过期时间(秒)")


class TokenData(BaseModel):
    """令牌数据模式"""
    user_id: Optional[int] = None
    username: Optional[str] = None
    scopes: list[str] = []


class PasswordReset(BaseModel):
    """密码重置模式"""
    email: EmailStr = Field(..., description="邮箱地址")


class PasswordResetConfirm(BaseModel):
    """密码重置确认模式"""
    token: str = Field(..., description="重置令牌")
    new_password: str = Field(..., min_length=8, max_length=100, description="新密码")
    confirm_password: str = Field(..., description="确认新密码")
    
    def passwords_match(self) -> bool:
        """验证密码是否匹配"""
        return self.new_password == self.confirm_password


class PasswordChange(BaseModel):
    """密码修改模式"""
    current_password: str = Field(..., description="当前密码")
    new_password: str = Field(..., min_length=8, max_length=100, description="新密码")
    confirm_password: str = Field(..., description="确认新密码")
    
    def passwords_match(self) -> bool:
        """验证密码是否匹配"""
        return self.new_password == self.confirm_password